
// Definition for a binary tree node.
function TreeNode(val) {
    this.val = val;
    this.left = this.right = null;
}

/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isSymmetric = function(root) {
  if(!root) return true;
  var mirrorTree = function(rootParam) {
    function mirror(node) {
      if(!node) return;
      let temp = node.left;
      node.left = node.right;
      node.right = temp;
      mirror(node.left);
      mirror(node.right);
    }
    mirror(rootParam);
    return rootParam;
  };
  let flag = true;
  function equal(node1, node2) {
    if(!node1 && !node2 || !flag) return;
    else if(node1 && node2) {
      if(node1.val != node2.val) {
        flag = false;
        return;
      } else {
        equal(node1.left, node2.left);
        equal(node1.right, node2.right);
      }
    } else {
      flag = false;
    }
  }

  let left = mirrorTree(root.left);
  equal(left, root.right);
  return flag;
};
